package com.fuershiye.woman.monitor.controller;

import com.fuershiye.woman.monitor.beans.PageResult;
import com.fuershiye.woman.monitor.beans.Result;
import com.fuershiye.woman.monitor.beans.ViewReportAllValue;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.Indicator;
import com.fuershiye.woman.monitor.service.IndicatorService;
import com.fuershiye.woman.monitor.service.ReportService;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 按年份进行报表的查询
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/report/stat")
public class ReportStatController {

	@Autowired
	ReportService reportService;
	@Autowired
	IndicatorService indicatorService;

//	@RequiresPermissions("report:statView")
	@RequestMapping(value = "/statView.do")
	public String statView(String show, Model model) {

		List<Integer> years = DateFormatUtil.getAreaYears();
		model.addAttribute("years", years);

		return "stat/" + show;
	}

	/**
	 * 只查询仅仅是国家指标的数据
	 * 
	 * @param year 【必传】
	 * @param page	【非必传】
	 * @param pageSize	【非必传】
	 * 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	//	@RequiresPermissions("report:preAlarm")
	@RequestMapping(value = "/country.do")
	@ResponseBody
	public Result country(Long formsid, Integer year, Integer page, Integer pageSize) {
		
		if(!DateFormatUtil.isYear(year)||formsid==null||formsid==0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		//该指标是否是国家指标	0:不是(默认);1:是
		int country = 1;
		List<Indicator> ilist = indicatorService.listByCondition("", country, formsid,null,null,1, null);
		List<Long> indicatorIds = indicatorService.getIndicatorId(ilist);

		List<Integer> years = Lists.newArrayList();
		years.add(year);
		List<ViewReportAllValue> reportList = reportService.getReportDate(null, formsid, indicatorIds, years, 1);

		List<Map<String, Object>> result = reportService.filter(reportList, years);
		
		return new Result(Massage.SUCCESS, new PageResult(result, reportList.size()));
	}
	
}
